!which python
/Users/shubhang/Documents/Course/pgp-aiml-oct-21/FMT-Project/FMT-Project/venv/bin/python
!pip install numpy
Requirement already satisfied: numpy in ./venv/lib/python3.8/site-packages (1.22.3)
WARNING: You are using pip version 21.3.1; however, version 22.0.4 is available.
You should consider upgrading via the '/Users/shubhang/Documents/Course/pgp-aiml-oct-21/FMT-Project/FMT-Project/venv/bin/python -m pip install --upgrade pip' command.
!pip install matplotlib
Requirement already satisfied: matplotlib in ./venv/lib/python3.8/site-packages (3.5.1)
Requirement already satisfied: python-dateutil>=2.7 in ./venv/lib/python3.8/site-packages (from matplotlib) (2.8.2)
Requirement already satisfied: pyparsing>=2.2.1 in ./venv/lib/python3.8/site-packages (from matplotlib) (3.0.7)
Requirement already satisfied: cycler>=0.10 in ./venv/lib/python3.8/site-packages (from matplotlib) (0.11.0)
Requirement already satisfied: packaging>=20.0 in ./venv/lib/python3.8/site-packages (from matplotlib) (21.3)
Requirement already satisfied: numpy>=1.17 in ./venv/lib/python3.8/site-packages (from matplotlib) (1.22.3)
Requirement already satisfied: pillow>=6.2.0 in ./venv/lib/python3.8/site-packages (from matplotlib) (9.1.0)
Requirement already satisfied: kiwisolver>=1.0.1 in ./venv/lib/python3.8/site-packages (from matplotlib) (1.4.2)
Requirement already satisfied: fonttools>=4.22.0 in ./venv/lib/python3.8/site-packages (from matplotlib) (4.31.2)
Requirement already satisfied: six>=1.5 in ./venv/lib/python3.8/site-packages (from python-dateutil>=2.7->matplotlib) (1.16.0)
WARNING: You are using pip version 21.3.1; however, version 22.0.4 is available.
You should consider upgrading via the '/Users/shubhang/Documents/Course/pgp-aiml-oct-21/FMT-Project/FMT-Project/venv/bin/python -m pip install --upgrade pip' command.
from matplotlib import pyplot as plt
import numpy as np
story_img_1 = plt.imread('Experiment/1.jpeg')
type(story_img_1)
numpy.ndarray
story_img_1.shape
(300, 600, 3)
plt.imshow(story_img_1)
<matplotlib.image.AxesImage at 0x1203e5160>
story_imgs = []
import glob
story_imgs = glob.glob('Experiment/*')
story_imgs
['Experiment/93.1.jpeg', 'Experiment/1.jpeg', 'Experiment/9.jpg', 'Experiment/98.jpeg', 'Experiment/94.jpeg', 'Experiment/97.gif', 'Experiment/6.jpeg', 'Experiment/7.jpeg', 'Experiment/99.jpeg', 'Experiment/8.jpeg', 'Experiment/4.jpeg', 'Experiment/93.0.jpg', 'Experiment/5.jpeg', 'Experiment/95.jpg', 'Experiment/2.jpeg', 'Experiment/92.jpg', 'Experiment/91.jpeg', 'Experiment/3.jpeg']
story_img_arr = []
story_imgs.sort()
story_imgs
['Experiment/1.jpeg', 'Experiment/2.jpeg', 'Experiment/3.jpeg', 'Experiment/4.jpeg', 'Experiment/5.jpeg', 'Experiment/6.jpeg', 'Experiment/7.jpeg', 'Experiment/8.jpeg', 'Experiment/9.jpg', 'Experiment/91.jpeg', 'Experiment/92.jpg', 'Experiment/93.0.jpg', 'Experiment/93.1.jpeg', 'Experiment/94.jpeg', 'Experiment/95.jpg', 'Experiment/97.gif', 'Experiment/98.jpeg', 'Experiment/99.jpeg']
len(story_imgs)
18
rows = 3
columns = 6
w = 10
h = 10
for idx, image_path in enumerate(story_imgs):
img = plt.imread(image_path)
story_img_arr.append(img)
fig = plt.figure(figsize=(10, 20))
columns = 3
rows = 6
for i in range(1, columns*rows +1):
img = story_img_arr[i-1]
fig.add_subplot(rows, columns, i)
plt.title(story_imgs[i-1])
plt.imshow(img)
plt.show()
dialogues = [
"Thor use to leave his hammer and helmet anywhere, he knows no one is worthy to pick up",
"Ash's Pickachu notices it one day",
"He had never seen such a fancy and shiny thing, and became happy",
"Unknowingly he picked them up and started dancing",
"Thor came to the place and didn't like that an unknown creature is worthy!!",
"Give back my Mjolnir!! It's not a toy!",
"Pickachu didn't like this and became angry",
"Steve: Seems this strange creature is going to attack!",
"Pickachu attacks with thunderbolt",
"Thor: THANK YOU, SWEET RABBIT. I'm not in a gaming mood!!",
"Thor: You’re spark is big. I’ve fought bigger.",
"Both collides, Boom!!",
"Pickachu teases with funny faces",
"I like this small creature, he's brave and courageous",
"Pickachu on hearing these compliments also gives up anger"
]
img2 = story_img_arr[1]
plt.imshow(img2)
<matplotlib.image.AxesImage at 0x120aded30>
img2.shape
(477, 636, 3)
def crop(arr, width_start, width_end, height_start, height_end):
result = arr[height_start:height_end, width_start:width_end, :]
return result
plt.imshow(crop(img2, 0, 430, 0, 477))
<matplotlib.image.AxesImage at 0x120b46820>
temp_img = story_img_arr[14]
plt.imshow(temp_img)
<matplotlib.image.AxesImage at 0x120ba8f40>
temp_img.shape
(349, 620, 3)
plt.imshow(crop(temp_img, 200, 450, 240, 330))
<matplotlib.image.AxesImage at 0x120c1c100>
def add_whitespace_area(arr, height, at_bottom=True, add_white = True):
width = arr.shape[1]
area = np.zeros(shape=(height, width, 3), dtype=int)
white_space = 255-area if add_white else area
return np.vstack((arr, white_space)) if at_bottom else np.vstack((white_space, arr))
plt.imshow(add_whitespace_area(img2, 100, False))
<matplotlib.image.AxesImage at 0x120c733d0>
plt.imshow(add_whitespace_area(img2, 100))
<matplotlib.image.AxesImage at 0x120cb9f10>
from PIL import Image, ImageFont, ImageDraw
sample_img = add_whitespace_area(img2, 100)
plt.imshow(sample_img)
<matplotlib.image.AxesImage at 0x1212400a0>
from matplotlib import cm
im = Image.fromarray(np.uint8(sample_img))
fontsize = 20
font = ImageFont.truetype(font='./twinkle-star/TwinkleStar-Regular.ttf', size=40)
draw = ImageDraw.Draw(im)
draw.text((0,500), "This is a test", (0,0,0), font=font)
plt.imshow(im)
<matplotlib.image.AxesImage at 0x12044efa0>
im.show()
draw.text?
def write_text(arr, width, height, text, font_size):
changed_image = arr.copy()
im = Image.fromarray(np.uint8(changed_image))
draw = ImageDraw.Draw(im)
font = ImageFont.truetype(font='./twinkle-star/TwinkleStar-Regular.ttf', size=font_size)
draw.text((width,height), text, (0,0,0), font=font)
return im
plt.imshow(write_text(sample_img, 10, 500, dialogues[1], 50))
<matplotlib.image.AxesImage at 0x121e3c6d0>
dialogues[1]
"Ash's Pickachu notices it"
len(dialogues)
15
attack_middle = crop(temp_img, 200, 450, 240, 330)
plt.imshow(attack_middle)
<matplotlib.image.AxesImage at 0x121e9eaf0>
attack_middle.shape
(90, 250, 3)
plt.imshow(np.fliplr(attack_middle))
<matplotlib.image.AxesImage at 0x121f0b7c0>
img_flip = attack_middle[:, ::-1 , :]
plt.imshow(img_flip)
<matplotlib.image.AxesImage at 0x121f6e970>
plt.imshow(attack_middle)
<matplotlib.image.AxesImage at 0x121fd3580>
def concatenate_images(array_images):
return np.hstack(array_images)
story_img_arr[12].shape
(249, 602, 3)
img_flip.shape
(90, 250, 3)
story_img_arr[13].shape
(168, 299, 3)
merged_area = add_whitespace_area(add_whitespace_area(img_flip,
79,
add_white=False),
80,
at_bottom=False,
add_white=False)
merged_area.shape
(249, 250, 3)
right_area = add_whitespace_area(add_whitespace_area(story_img_arr[13],
40,
add_white=False),
41,
at_bottom=False,
add_white=False)
plt.imshow(right_area)
<matplotlib.image.AxesImage at 0x12220ac40>
plt.imshow(concatenate_images([story_img_arr[12], merged_area, right_area]))
<matplotlib.image.AxesImage at 0x1222509a0>
plt.imshow(concatenate_images([story_img_arr[12], right_area]))
<matplotlib.image.AxesImage at 0x1222ab850>
plt.imshow(np.fliplr(story_img_arr[9]))
<matplotlib.image.AxesImage at 0x12230a220>
pick_attack = np.fliplr(story_img_arr[9])
plt.imshow(pick_attack)
<matplotlib.image.AxesImage at 0x12236be20>
pick_attack.shape
(163, 309, 3)
plt.imshow(story_img_arr[12])
<matplotlib.image.AxesImage at 0x1223d3370>
story_img_arr[12].shape
(249, 602, 3)
right_area = add_whitespace_area(add_whitespace_area(pick_attack,
43,
add_white=False),
43,
at_bottom=False,
add_white=False)
plt.imshow(concatenate_images([story_img_arr[12], right_area]))
<matplotlib.image.AxesImage at 0x12242bee0>
plt.imshow(story_img_arr[11])
<matplotlib.image.AxesImage at 0x122492310>
story_img_arr[11].shape
(163, 390, 3)
height_start = 30
height_end = 193
width_start = 375
width_end = 600
middle_attack = crop(story_img_arr[12], width_start, width_end, height_start, height_end)
plt.imshow(middle_attack)
<matplotlib.image.AxesImage at 0x1240a9850>
middle_attack.shape
(163, 220, 3)
plt.imshow(concatenate_images([story_img_arr[11], middle_attack, pick_attack]))
<matplotlib.image.AxesImage at 0x1243e2cd0>
both_attack = concatenate_images([story_img_arr[11], middle_attack, pick_attack])
plt.imshow(add_whitespace_area(both_attack, 100))
<matplotlib.image.AxesImage at 0x1236a6ac0>
both_attack_with_caption = add_whitespace_area(both_attack, 130)
plt.imshow(both_attack_with_caption)
<matplotlib.image.AxesImage at 0x123b03f10>
plt.imshow(write_text(both_attack_with_caption,
0, 140,
"""
Pickachu attacks with thunderbolt,
Thor Says: Thank you, Sweet RABBIT.
I'm not in a gaming mood!!"""
, 30))
<matplotlib.image.AxesImage at 0x1245b9f10>
dialogues
['Thor use to leave his hammer and helmet anywhere, he knows no one is worthy to pick up', "Ash's Pickachu notices it", 'He had never seen such a fancy and shiny thing, and became happy', 'Unknowingly he picked them up and started dancing', "Thor came to the place and didn't like that an unknown creature is worthy!!", "Give back my Mjolnir!! It's not a toy!", "Pickachu didn't like this and became angry", 'Steve: Seems this strange creature is going to attack!', 'Pickachu attacks with thunderbolt', "Thor: THANK YOU, SWEET RABBIT. I'm not in a gaming mood!!", 'Thor: You’re spark is big. I’ve fought bigger.', 'Both collides, Boom!!', 'Pickachu teases with funny faces', "I like this small creature, he's brave and courageous", 'Pickachu on hearing these compliments also gives up anger']
attack_scene = write_text(both_attack_with_caption,
0, 140,
"""
Pickachu attacks with thunderbolt,
Thor Says: Thank you, Sweet RABBIT.
I'm not in a gaming mood!!"""
, 30)
plt.imshow(story_img_arr[0])
<matplotlib.image.AxesImage at 0x124828940>
story_img_arr[0].shape
(300, 600, 3)
scene_1 = add_whitespace_area(story_img_arr[0], 130)
plt.imshow(scene_1)
<matplotlib.image.AxesImage at 0x125090580>
scene_1 = write_text(scene_1,
10, 300,
"""Mighty Thor use to leave his hammer and
helmet anywhere, he knows no one
is worthy to pick it up!!
"""
, 30)
plt.imshow(scene_1)
<matplotlib.image.AxesImage at 0x1250fa790>
scene_1.save("out/scene1.jpg")
plt.imshow(write_text(sample_img, 10, 500, dialogues[1], 40))
<matplotlib.image.AxesImage at 0x1252335b0>
scene_2 = write_text(sample_img, 10, 500, dialogues[1], 40)
scene_2.save('out/scene_2.jpg')
scene_3 = add_whitespace_area(story_img_arr[2], 120)
scene_3 = write_text(scene_3,
10, 250,
"""He had never seen such
a fancy and shiny thing,
and became happy"""
, 20)
plt.imshow(scene_3)
<matplotlib.image.AxesImage at 0x128f48280>
scene_3.save('out/scene_3.jpg')
scene_4 = add_whitespace_area(story_img_arr[3], 80)
scene_4 = write_text(scene_4,
50, 310,
"""Unknowingly he picked them up
and started dancing"""
, 20)
plt.imshow(scene_4)
<matplotlib.image.AxesImage at 0x1293142b0>
scene_4.save('out/scene4.jpg')
plt.imshow(story_img_arr[4])
<matplotlib.image.AxesImage at 0x129367520>
story_img_arr[4].shape
(415, 625, 3)
plt.imshow(crop(story_img_arr[4], 120, 600, 0, 415))
<matplotlib.image.AxesImage at 0x129353070>
scene_5 = crop(story_img_arr[4], 120, 600, 0, 415)
scene_5 = add_whitespace_area(scene_5, 120)
scene_5 = write_text(scene_5,
40, 415,
"""Thor came to the place and
didn't like that an
unknown creature is worthy!!"""
, 30)
plt.imshow(scene_5)
scene_5.save('out/scene5.jpg')
plt.imshow(story_img_arr[5])
<matplotlib.image.AxesImage at 0x129741730>
story_img_arr[5].shape
(143, 352, 3)
scene_6 = crop(story_img_arr[5], 150, 370, 0, 143)
scene_6 = add_whitespace_area(scene_6, 30)
scene_6 = write_text(scene_6,
5, 143,
"""Give back my Mjolnir!!"""
, 20)
plt.imshow(scene_6)
scene_6.save('out/scene6.jpg')
plt.imshow(story_img_arr[6])
<matplotlib.image.AxesImage at 0x129fd2a60>
story_img_arr[6].shape
(422, 767, 3)
scene_7 = crop(story_img_arr[6], 150, 700, 0, 422)
scene_7 = add_whitespace_area(scene_7, 60)
scene_7 = write_text(scene_7,
5, 422,
"""It's not a toy! Strange creature!!"""
, 40)
plt.imshow(scene_7)
scene_7.save('out/scene7.jpg')
plt.imshow(story_img_arr[7])
<matplotlib.image.AxesImage at 0x12a4f9160>
story_img_arr[7].shape
(1622, 2880, 3)
scene_8 = add_whitespace_area(story_img_arr[7], 200)
scene_8 = write_text(scene_8,
5, 1622,
"""Pickachu didn't like this and became angry!!"""
, 150)
plt.imshow(scene_8)
scene_8.save('out/scene_8.jpg')
plt.imshow(story_img_arr[8])
<matplotlib.image.AxesImage at 0x12a0f5ca0>
story_img_arr[8].shape
(630, 1200, 3)
scene_9 = add_whitespace_area(story_img_arr[8], 150)
scene_9 = write_text(scene_9,
50, 630,
"""Steve say's watch out!!
Seems this strange creature is going to attack!"""
, 55)
plt.imshow(scene_9)
scene_9.save('out/scene_9.jpg')
# Run back home, little princess.
# "I've got this completely under control!"
plt.imshow(story_img_arr[10])
<matplotlib.image.AxesImage at 0x12a4469a0>
story_img_arr[10].shape
(1080, 1920, 3)
scene_10 = add_whitespace_area(story_img_arr[10], 150)
scene_10 = write_text(scene_10,
50, 1080,
"""I've got this completely under control!"""
, 100)
plt.imshow(scene_10)
scene_10.save('out/scene10.jpg')
plt.imshow(attack_scene)
<matplotlib.image.AxesImage at 0x12a36bbb0>
attack_scene.save('out/scene11.jpg')
pickachu_teasing1 = plt.imread('Experiment/97.1.jpg')
plt.imshow(pickachu_teasing1)
<matplotlib.image.AxesImage at 0x129c45310>
pickachu_teasing2 = plt.imread('Experiment/97.2.jpg')
plt.imshow(pickachu_teasing2)
<matplotlib.image.AxesImage at 0x12a73a0d0>
plt.imshow(story_img_arr[15])
<matplotlib.image.AxesImage at 0x12a37cf70>
pickachu_after_attack = np.hstack([pickachu_teasing2, pickachu_teasing1])
plt.imshow(pickachu_after_attack)
<matplotlib.image.AxesImage at 0x12a83c790>
pickachu_after_attack.shape
(378, 996, 3)
scene_12 = add_whitespace_area(pickachu_after_attack, 100)
scene_12 = write_text(scene_12,
50, 378,
"""Pickachu teases with funny faces!!"""
, 65)
plt.imshow(scene_12)
scene_12.save('out/scene12.jpg')
plt.imshow(story_img_arr[-2])
<matplotlib.image.AxesImage at 0x12ac141c0>
story_img_arr[-2].shape
(576, 500, 3)
scene_13 = add_whitespace_area(story_img_arr[-2], 100)
scene_13 = write_text(scene_13,
10, 576,
"""I like this small creature,
he's brave and courageous"""
, 40)
plt.imshow(scene_13)
scene_13.save('out/scene13.jpg')
plt.imshow(story_img_arr[-1])
<matplotlib.image.AxesImage at 0x12ae3aa90>
story_img_arr[-1].shape
(225, 300, 3)
scene_14 = add_whitespace_area(story_img_arr[-1], 120)
scene_14 = write_text(scene_14,
10, 225,
"""Pickachu on hearing
these compliments
also gives up anger"""
, 30)
plt.imshow(scene_14)
scene_14.save('out/scene_14.jpg')
story_img_arr[15].shape
(378, 498, 4)
all_outputs = glob.glob('out/*')
all_outputs.sort()
all_outputs
['out/1.jpg', 'out/2.jpg', 'out/3.jpg', 'out/4.jpg', 'out/5.jpg', 'out/6.jpg', 'out/7.jpg', 'out/8.jpg', 'out/9.jpg', 'out/91.jpg', 'out/92.jpg', 'out/93.jpg', 'out/94.jpg', 'out/95.jpg']
fig = plt.figure(figsize=(10, 25))
columns = 2
rows = 7
for i in range(1, columns*rows +1):
output_image = plt.imread(all_outputs[i-1])
img = output_image
fig.add_subplot(rows, columns, i)
plt.title(f'Image {i}')
plt.imshow(img)
fig1 = plt.gcf()
plt.show()
plt.draw()
fig1.savefig('foo.jpg', dpi=100)
<Figure size 432x288 with 0 Axes>
!pip freeze > requirements.txt